<template>
  <div class="app-container">
    <!-- 左侧树形菜单 -->
    <div class="tree-panel">
      <div class="head-container">
        <el-input
          v-model="projectName"
          placeholder="请输入培训项目"
          clearable
          size="small"
          prefix-icon="el-icon-search"
          style="margin-bottom: 20px"
          @keyup.enter.native="filterTree"
        />
      </div>
      <el-tree
        ref="tree"
        :data="projectOptions"
        :props="defaultProps"
        :expand-on-click-node="false"
        :filter-node-method="filterNode"
        node-key="id"
        default-expand-all
        highlight-current
        @node-click="handleNodeClick"
      />
    </div>

    <!-- 右侧表格 -->
    <el-col :span="20" style="padding: 20px">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="80px"
        :gutter="10"
      >
        <el-row :gutter="20">
          <!-- 姓名 -->
          <el-col :span="6">
            <el-form-item label="姓名" prop="studentName">
              <el-input
                v-model="queryParams.studentName"
                placeholder="请输入姓名"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>

          <!-- 编号 -->
          <el-col :span="6">
            <el-form-item label="编号" prop="examNumber">
              <el-input
                v-model="queryParams.examNumber"
                placeholder="请输入编号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>

          <!-- 证件编号 -->
          <el-col :span="6">
            <el-form-item label="证件编号" prop="idNumber">
              <el-input
                v-model="queryParams.idNumber"
                placeholder="请输入证件编号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>

          <!-- 培训类型 -->
          <el-col :span="6">
            <el-form-item label="培训类型" prop="trainingType">
              <el-select
                v-model="queryParams.trainingType"
                placeholder="选择培训类型"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.training_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <!-- 手机号码 -->
          <el-col :span="6">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input
                v-model="queryParams.phonenumber"
                placeholder="请输入手机号码"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>

          <!-- 单位名称 -->
          <el-col :span="6">
            <el-form-item label="单位名称" prop="unitName">
              <el-input
                v-model="queryParams.unitName"
                placeholder="请输入单位名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>

          <!-- 考试时间 -->
          <el-col :span="6">
            <el-form-item label="考试时间" prop="examTime">
              <el-date-picker
                v-model="queryParams.examTime"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                clearable
                @keyup.enter.native="handleQuery"
                style="width: 210px"
              />
            </el-form-item>
          </el-col>

          <!-- 是否通过 -->
          <el-col :span="6">
            <el-form-item label="是否通过" prop="examResult">
              <el-select
                v-model="queryParams.examResult"
                placeholder="选择是否通过"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.exam_result"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <!-- 考试状态 -->
          <el-col :span="6">
            <el-form-item label="考试状态" prop="examStatus">
              <el-select
                v-model="queryParams.examStatus"
                placeholder="选择考试状态"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.exam_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <!-- 补考情况 -->
          <el-col :span="6">
            <el-form-item label="补考情况" prop="makeUpStatus">
              <el-select
                v-model="queryParams.makeUpStatus"
                placeholder="请选择补考情况"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.make_up_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <!-- 补考标记 -->
          <el-col :span="6">
            <el-form-item label="补考标记" prop="makeUpMark">
              <el-select
                v-model="queryParams.makeUpMark"
                placeholder="选择补考标记"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.make_up_mark"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <!-- 备注 -->
          <el-col :span="6">
            <el-form-item label="备注" prop="examEecordRemark">
              <el-input
                v-model="queryParams.examEecordRemark"
                placeholder="请输入备注"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <!-- 培训班 -->
          <el-col :span="6">
            <el-form-item label="培训班" prop="trainingClassName">
              <el-input
                v-model="queryParams.trainingClassName"
                placeholder="请输入培训班"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>

          <!-- 电子证书 -->
          <el-col :span="6">
            <el-form-item label="电子证书" prop="electronicCertificate">
              <el-select
                v-model="queryParams.electronicCertificate"
                placeholder="选择电子证书标记"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.electronic_certificate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <!-- 操作按钮 -->
          <el-col :span="12">
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                size="mini"
                @click="handleQuery"
                >搜索</el-button
              >
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
                >重置</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="24">
          <div
            style="
              display: flex;
              justify-content: space-between;
              align-items: center;
            "
          >
            <!-- 左侧按钮 -->
            <div style="display: flex; gap: 10px">
              <el-button type="warning" plain @click="handleExport"
                >导出</el-button
              >
              <el-button type="info" plain @click="handleRemark"
                >学员备注</el-button
              >
              <el-button type="success" plain @click="handleNotice"
                >学员通知</el-button
              >
              <el-button type="primary" plain @click="handleMakeUp"
                >多次补考</el-button
              >
              <el-button type="danger" plain @click="handleExamDetail"
                >考试详情</el-button
              >
              <el-button type="warning" plain @click="handleGenerateCertificate"
                >生成电子证书</el-button
              >
            </div>

            <!-- 右侧按钮 -->
            <div style="display: flex; gap: 10px">
              <column-config-dialog-vue
                :showSearch.sync="showSearch"
                :columns="tableColumns"
                @queryTable="getList"
              ></column-config-dialog-vue>
            </div>
          </div>
        </el-col>
      </el-row>

      <el-table
        v-loading="loading"
        :data="examRecordList"
        @selection-change="handleSelectionChange"
        border
        style="width: 100%"
      >
        <!-- 选择列 -->
        <el-table-column type="selection" width="55" align="center" />

        <!-- 编号 -->
        <el-table-column
          label="编号"
          align="center"
          prop="examNumber"
          v-if="tableColumns.find((col) => col.key === 'examNumber').visible"
          width="80"
        />

        <!-- 姓名 -->
        <el-table-column
          label="姓名"
          align="center"
          prop="studentName"
          v-if="tableColumns.find((col) => col.key === 'studentName').visible"
          width="80"
        />

        <!-- 证件编号 -->
        <el-table-column
          label="证件编号"
          align="center"
          prop="idNumber"
          v-if="tableColumns.find((col) => col.key === 'idNumber').visible"
          width="120"
        />

        <!-- 性别 -->
        <el-table-column
          label="性别"
          align="center"
          prop="sex"
          v-if="tableColumns.find((col) => col.key === 'sex').visible"
          width="60"
        />

        <!-- 电话 -->
        <el-table-column
          label="电话"
          align="center"
          prop="phonenumber"
          v-if="tableColumns.find((col) => col.key === 'phonenumber').visible"
          width="120"
        />

        <!-- 单位名称 -->
        <el-table-column
          label="单位名称"
          align="center"
          prop="unitName"
          v-if="tableColumns.find((col) => col.key === 'unitName').visible"
          width="120"
        />

        <!-- 培训项目 -->
        <el-table-column
          label="培训项目"
          align="center"
          prop="trainingProjectName"
          v-if="
            tableColumns.find((col) => col.key === 'trainingProjectName')
              .visible
          "
          width="120"
        />

        <!-- 培训班编号 -->
        <el-table-column
          label="培训班编号"
          align="center"
          prop="trainingClassCode"
          v-if="
            tableColumns.find((col) => col.key === 'trainingClassCode').visible
          "
          width="100"
        />

        <!-- 培训班 -->
        <el-table-column
          label="培训班"
          align="center"
          prop="trainingClassName"
          v-if="
            tableColumns.find((col) => col.key === 'trainingClassName').visible
          "
          width="100"
        />

        <!-- 考试安排开始时间 -->
        <el-table-column
          label="考试安排开始时间"
          align="center"
          prop="examStartTime"
          v-if="tableColumns.find((col) => col.key === 'examStartTime').visible"
          width="140"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.examStartTime, "{y}-{m}-{d} {h}:{i}:{s}")
            }}</span>
          </template>
        </el-table-column>

        <!-- 考试安排结束时间 -->
        <el-table-column
          label="考试安排结束时间"
          align="center"
          prop="examEndTime"
          v-if="tableColumns.find((col) => col.key === 'examEndTime').visible"
          width="140"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.examEndTime, "{y}-{m}-{d} {h}:{i}:{s}")
            }}</span>
          </template>
        </el-table-column>

        <!-- 考试状态 -->
        <el-table-column
          label="考试状态"
          align="center"
          prop="examStatus"
          v-if="tableColumns.find((col) => col.key === 'examStatus').visible"
          width="100"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.exam_status"
              :value="scope.row.examStatus"
            />
          </template>
        </el-table-column>

        <!-- 考试次数 -->
        <el-table-column
          label="考试次数"
          align="center"
          prop="examFrequency"
          v-if="tableColumns.find((col) => col.key === 'examFrequency').visible"
          width="80"
        />

        <!-- 考试成绩 -->
        <el-table-column
          label="考试成绩"
          align="center"
          prop="totalScore"
          v-if="tableColumns.find((col) => col.key === 'totalScore').visible"
          width="80"
        />

        <!-- 是否通过 -->
        <el-table-column
          label="是否通过"
          align="center"
          prop="examResult"
          v-if="tableColumns.find((col) => col.key === 'examResult').visible"
          width="80"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.examResult === '1'" class="text-green"
              >通过</span
            >
            <span v-else class="text-red">未通过</span>
          </template>
        </el-table-column>

        <!-- 补考情况 -->
        <el-table-column
          label="补考情况"
          align="center"
          prop="makeUpStatus"
          v-if="tableColumns.find((col) => col.key === 'makeUpStatus').visible"
          width="100"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.make_up_status"
              :value="scope.row.makeUpStatus"
            />
          </template>
        </el-table-column>

        <!-- 补考标记 -->
        <el-table-column
          label="补考标记"
          align="center"
          prop="makeUpMark"
          v-if="tableColumns.find((col) => col.key === 'makeUpMark').visible"
          width="100"
        />

        <!-- 生产电子证书 -->
        <el-table-column
          label="生产电子证书"
          align="center"
          prop="certificateStatus"
          v-if="
            tableColumns.find((col) => col.key === 'certificateStatus').visible
          "
          width="100"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              plain
              @click="handleGenerateCertificate(scope.row)"
            >
              生成
            </el-button>
          </template>
        </el-table-column>

        <!-- 备注 -->
        <el-table-column
          label="备注"
          align="center"
          prop="examRecordRemark"
          v-if="
            tableColumns.find((col) => col.key === 'examRecordRemark').visible
          "
          width="120"
        />

        <!-- 操作 -->
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          v-if="tableColumns.find((col) => col.key === 'operation').visible"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleExamDetail(scope.row)"
            >
              考试详情
            </el-button>
            <el-button size="mini" type="text" @click="handleRemark(scope.row)">
              备注
            </el-button>
            <el-button size="mini" type="text" @click="handleNotice(scope.row)">
              学员通知
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改考试记录对话框 -->
      <el-dialog
        :title="title"
        :visible.sync="open"
        width="500px"
        append-to-body
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="试卷id" prop="examPaperId">
            <el-input v-model="form.examPaperId" placeholder="请输入试卷id" />
          </el-form-item>
          <el-form-item label="考生用户ID" prop="studentId">
            <el-input v-model="form.studentId" placeholder="请输入考生用户ID" />
          </el-form-item>
          <el-form-item label="考试编号" prop="examNumber">
            <el-input v-model="form.examNumber" placeholder="请输入考试编号" />
          </el-form-item>
          <el-form-item label="考试开始时间" prop="examStartTime">
            <el-date-picker
              clearable
              v-model="form.examStartTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择考试开始时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="考试结束时间" prop="examEndTime">
            <el-date-picker
              clearable
              v-model="form.examEndTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择考试结束时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="实际考试时长(分钟)" prop="actualDuration">
            <el-input
              v-model="form.actualDuration"
              placeholder="请输入实际考试时长(分钟)"
            />
          </el-form-item>
          <el-form-item label="考试总分" prop="totalScore">
            <el-input v-model="form.totalScore" placeholder="请输入考试总分" />
          </el-form-item>
          <el-form-item label="理论成绩" prop="theoryResult">
            <el-select v-model="form.theoryResult" placeholder="请选择理论成绩">
              <el-option
                v-for="dict in dict.type.exam_result"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="实操成绩" prop="practicalResult">
            <el-select
              v-model="form.practicalResult"
              placeholder="请选择实操成绩"
            >
              <el-option
                v-for="dict in dict.type.exam_result"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="补考成绩" prop="makeUpResult">
            <el-select v-model="form.makeUpResult" placeholder="请选择补考成绩">
              <el-option
                v-for="dict in dict.type.exam_result"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="考试结果" prop="examResult">
            <el-select v-model="form.examResult" placeholder="请选择考试结果">
              <el-option
                v-for="dict in dict.type.exam_result"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="补考标记" prop="makeUpMark">
            <el-input v-model="form.makeUpMark" placeholder="请输入补考标记" />
          </el-form-item>
          <el-form-item label="补考情况" prop="makeUpStatus">
            <el-select v-model="form.makeUpStatus" placeholder="请选择补考情况">
              <el-option
                v-for="dict in dict.type.make_up_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否可二次考试申报" prop="isSecondExam">
            <el-select
              v-model="form.isSecondExam"
              placeholder="请选择是否可二次考试申报"
            >
              <el-option
                v-for="dict in dict.type.is_second_exam"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="考试状态" prop="examStatus">
            <el-select v-model="form.examStatus" placeholder="请选择考试状态">
              <el-option
                v-for="dict in dict.type.exam_result"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="提交类型" prop="submitType">
            <el-input v-model="form.submitType" placeholder="请输入提交类型" />
          </el-form-item>
          <el-form-item label="考试终端IP地址" prop="examIp">
            <el-input
              v-model="form.examIp"
              placeholder="请输入考试终端IP地址"
            />
          </el-form-item>
          <el-form-item label="考试终端坐标" prop="examCoordinates">
            <el-input
              v-model="form.examCoordinates"
              placeholder="请输入考试终端坐标"
            />
          </el-form-item>
          <el-form-item label="设备信息" prop="deviceInfo">
            <el-input v-model="form.deviceInfo" placeholder="请输入设备信息" />
          </el-form-item>
          <el-form-item label="备注" prop="examRecordRemark">
            <el-input
              v-model="form.examRecordRemark"
              placeholder="请输入备注"
            />
          </el-form-item>
          <el-form-item label="是否删除" prop="delFlag">
            <el-input v-model="form.delFlag" placeholder="请输入是否删除" />
          </el-form-item>
          <el-form-item label="${comment}" prop="exampleOne">
            <el-input
              v-model="form.exampleOne"
              placeholder="请输入${comment}"
            />
          </el-form-item>
          <el-form-item label="${comment}" prop="exampleTwo">
            <el-input
              v-model="form.exampleTwo"
              placeholder="请输入${comment}"
            />
          </el-form-item>
          <el-form-item label="${comment}" prop="exampleThree">
            <el-input
              v-model="form.exampleThree"
              placeholder="请输入${comment}"
            />
          </el-form-item>
          <el-form-item label="${comment}" prop="exampleFour">
            <el-input
              v-model="form.exampleFour"
              placeholder="请输入${comment}"
            />
          </el-form-item>
          <el-form-item label="${comment}" prop="exampleFive">
            <el-input
              v-model="form.exampleFive"
              placeholder="请输入${comment}"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <!-- 学员备注弹窗 -->
      <student-remark-dialog
        :visible.sync="remarkDialogVisible"
        :student-id="selectedStudentId"
        :student-name="selectedStudentName"
        @remark-updated="handleRemarkUpdated"
      />

      <!-- 学员通知弹窗 -->
      <student-notice-dialog
        :visible.sync="noticeDialogVisible"
        :class-id="currentClassId"
        @notice-sent="handleNoticeSent"
      />

      <!-- 考试详情弹窗 -->
      <exam-detail-dialog
        :visible.sync="examDetailDialogVisible"
        :student-id="selectedStudentId"
        :class-id="currentClassId"
        @update="handleExamDetailDialog"
      />
    </el-col>
  </div>
</template>

<script>
import {
  listExamRecord,
  getExamRecord,
  delExamRecord,
  addExamRecord,
  updateExamRecord,
  getExamList,
} from "@/api/examination/examRecord";
import { projectTreeSelect } from "@/api/training/projects";
import ColumnConfigDialogVue from "../../../components/ColumnConfig/ColumnConfigDialog.vue";
import studentRemarkDialog from "@/components/class/studentRemarkDialog.vue";
import studentNoticeDialog from "@/components/class/studentNoticeDialog.vue";
import examDetailDialog  from "@/components/class/examDetailDialog.vue";

export default {
  name: "ExamRecord",
  components: {
    ColumnConfigDialogVue,studentRemarkDialog,examDetailDialog,studentNoticeDialog
  },

  dicts: [
    "make_up_status", //补考情况
    "is_second_exam",
    "training_type", //培训类型
    "exam_result", //是否通过
    "exam_status", //考试状态
    // "is_second_exam",//是否可二次考试
    "make_up_mark", //补考标记
    // "", //电子证书标记
  ],
  data() {
    return {
      //弹出框
      remarkDialogVisible: false,
      noticeDialogVisible: false,
      examDetailDialogVisible: false,
      selectedStudentId: '',
      selectedStudentName: '',
      currentClassId: '',
      selection:null,

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考试记录表格数据
      examRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examNumber: null,
        examStartTime: null,
        examEndTime: null,
        theoryResult: null,
        practicalResult: null,
        makeUpResult: null,
        examResult: null,
        makeUpMark: null,
        makeUpStatus: null,
        isSecondExam: null,
        examStatus: null,

        studentName: null,
        idNumber: null,
        trainingType: null,
        phonenumber: null,
        unitName: null,
        examTime: null,
        examResult: null,
        examStatus: null,
        makeUpStatus: null,
        makeUpMark: null,
        examEecordRemark: null,
        trainingClassName: null,
        electronicCertificate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      //新增数据定义
      //树形结构数据
      // 所有部门树选项
      projectName: "",
      projectOptions: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 列配置弹窗
      tableColumns: [
        { key: "examNumber", label: "编号", visible: true },
        { key: "studentName", label: "姓名", visible: true },
        { key: "idNumber", label: "证件编号", visible: true },
        { key: "sex", label: "性别", visible: true },
        { key: "phonenumber", label: "电话", visible: true },
        { key: "unitName", label: "单位名称", visible: true },
        { key: "trainingProjectName", label: "培训项目", visible: true },
        { key: "trainingClassCode", label: "培训班编号", visible: true },
        { key: "trainingClassName", label: "培训班", visible: true },
        { key: "examStartTime", label: "考试安排开始时间", visible: true },
        { key: "examEndTime", label: "考试安排结束时间", visible: true },
        { key: "examStatus", label: "考试状态", visible: true },
        { key: "examFrequency", label: "考试次数", visible: true },
        { key: "totalScore", label: "考试成绩", visible: true },
        { key: "examResult", label: "是否通过", visible: true },
        { key: "makeUpStatus", label: "补考情况", visible: true },
        { key: "makeUpMark", label: "补考标记", visible: true },
        { key: "certificateStatus", label: "生产电子证书", visible: true },
        { key: "examRecordRemark", label: "备注", visible: true },
        { key: "operation", label: "操作", visible: true },
      ],
    };
  },
  created() {
    this.getList();
    this.getProjectTree();
  },
  methods: {
    /** 查询考试记录列表 */
    getList() {
      this.loading = true;
      getExamList(this.queryParams).then((response) => {
        this.examRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      // listExamRecord(this.queryParams).then((response) => {
      //   this.examRecordList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
    },
    handleRemarkUpdated() {
      // 备注更新后刷新数据
      this.remarkDialogVisible=false
      this.getList();
    },
    handleNoticeSent() {
      // 通知发送后处理
      this.noticeDialogVisible=false
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        examRecordId: null,
        examPaperId: null,
        studentId: null,
        examNumber: null,
        examStartTime: null,
        examEndTime: null,
        actualDuration: null,
        totalScore: null,
        theoryResult: null,
        practicalResult: null,
        makeUpResult: null,
        examResult: null,
        makeUpMark: null,
        makeUpStatus: null,
        isSecondExam: null,
        examStatus: null,
        submitType: null,
        examIp: null,
        examCoordinates: null,
        deviceInfo: null,
        examRecordRemark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        exampleOne: null,
        exampleTwo: null,
        exampleThree: null,
        exampleFour: null,
        exampleFive: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection =selection[0]
      this.ids = selection.map((item) => item.examRecordId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考试记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const examRecordId = row.examRecordId || this.ids;
      getExamRecord(examRecordId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考试记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.examRecordId != null) {
            updateExamRecord(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExamRecord(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const examRecordIds = row.examRecordId || this.ids;
      this.$modal
        .confirm('是否确认删除考试记录编号为"' + examRecordIds + '"的数据项？')
        .then(function () {
          return delExamRecord(examRecordIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "examination/examRecord/export",
        {
          ...this.queryParams,
        },
        `examRecord_${new Date().getTime()}.xlsx`
      );
    },

    //新增方法
    //树形结构
    filterTree() {
      this.$refs.tree.filter(this.projectName);
    },
    // 获取树形数据
    getProjectTree(val) {
      var params = { projectName: val };
      params.projectId = this.queryParams.trainingProjectId;
      projectTreeSelect(params).then((response) => {
        this.projectOptions = response.data;
      });
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    handleNodeClick(data) {
      this.queryParams.trainingProjectId = data.id;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 示例方法（实际逻辑需根据业务实现）
    handleRemark(row) {
      let targetRow =null;
      // 优先获取行数据，无则取选中数据
      if(row.studentId !==undefined){
        targetRow =row
      }else{
        targetRow = this.selection;
        if (!targetRow) return this.$message.warning('请选择一个学员');
      }
      this.selectedStudentId=targetRow.studentId
      this.selectedStudentName=targetRow.studentName
      this.remarkDialogVisible =true

    },
    handleNotice(row) {
      let targetRow =null;
      // 优先获取行数据，无则取选中数据
      if(row.studentId !==undefined){
        targetRow =row
      }else{
        targetRow = this.selection;
        if (!targetRow) return this.$message.warning('请选择一个学员');
      }
      this.currentClassId=targetRow.trainingClassId
      this.noticeDialogVisible =true
    },
    handleMakeUp() {
      this.$message.info("点击了多次补考");
    },
    handleExamDetail(row) {
      let targetRow =null;
      // 优先获取行数据，无则取选中数据
      if(row.studentId !==undefined){
        targetRow =row
      }else{
        targetRow = this.selection;
        if (!targetRow) return this.$message.warning('请选择一个学员');
      }
      this.currentClassId=targetRow.trainingClassId
      this.selectedStudentId=targetRow.studentId
      this.examDetailDialogVisible =true
    },
    handleExamDetailDialog(){
      this.examDetailDialogVisible =false,
      this.getList()
    },
    handleGenerateCertificate() {
      this.$message.info("点击了生成电子证书");
    },
  },
  watch: {
    projectName(val) {
      this.$refs.tree.filter(val);
      this.getProjectTree(val);
    },
  },
};
</script>
<style scoped>
.app-container {
  display: flex;
  height: 100%;
}

.tree-panel {
  width: 360px;
  border-right: 1px solid #ebeef5;
  overflow-y: auto;
  padding: 20px;
}
</style>





